
cc.Class({
    extends: cc.Component,

    properties: {

        lotteryPage:{
            type:cc.Node,
            default:null
        },
        turnPan:{
            type:cc.Node,
            default:null
        },
        resultPage:{
            type:cc.Node,
            default:null
        },
        heroHousePage:{
            type:cc.Node,
            default:null
        },
        Tip_label:{
            type:cc.Label,
            default:null
        },
        result_label:{
            type:cc.Label,
            default:null
        },
        GoldNum_label:{
            type:cc.Label,
            default:null
        },
    },


    onLoad () {
        this.heroPrice = [0,400,800,1200,1600,2000];//英雄价格
        this.initGoldNum();//初始化金币
        this.initIsHaveHero();//初始化拥有的英雄
        this.initHero();//初始化英雄殿
        this.freeLotteryNum = 3;//免费抽奖次数
        this.prizeArray = {
            "再来一次":[0,45],
            "全屏冰冻":[45,90],
            "一等奖":[90,135],
            "全屏爆破":[135,180],
            "钻石x1":[180,225],
            "全屏破甲":[225,270],
            "钻石x100":[270,315],
            "技能加血":[315,360],
        }
    },
    onclickBtn:function (target,data) {
        if (data == "start") {
            console.log('开始游戏');
        }
        else if (data == "lottery") {
            console.log('打开抽奖页');
            this.lotteryPage.active = true;
            this.turnPan.angle = 0;
            this.isOnLottery = false;
            this.turnRange = 0;
        }
        else if (data == "closeLottery") {
            console.log('关闭抽奖');
            this.lotteryPage.active = false;
        }
        else if (data == "startLottery") {//开始抽奖
           this.onLotteryBtn();
        }
        else if(data == "closeResultPage"){
            this.resultPage.active = false;
        }
        else if (data == "getPrize") {
            this.resultPage.active = false;
        }
        else if(data == "heroHouse"){
            console.log('打开英雄殿');
            this.heroHousePage.active = true;
        }
        else if(data == "closeHeroPage"){
            this.heroHousePage.active = false;
        }
        
    },
    initGoldNum:function () {
        this.GoldNum_label.string = Data.getGoldNum();
    },
    initIsHaveHero:function () {
        let arr = [1,0,0,0,0,0];
        if (localStorage.getItem("isHaveHero")) {
            let get = localStorage.getItem("isHaveHero")
            this.isHaveHero = JSON.parse(get)
        }
        else{
            localStorage.setItem("isHaveHero",JSON.stringify(arr));
            this.isHaveHero =arr;
        }
    },
    onBuyHero:function (target,data) {
        data = parseInt(data);
        if (Data.goldNum>=this.heroPrice[data]) {
            this.isHaveHero[data] = 1;
            Data.goldNum -= this.heroPrice[data];
            localStorage.setItem("goldNum",Data.goldNum); 
            localStorage.setItem("isHaveHero",JSON.stringify(this.isHaveHero));
            this.initGoldNum();
            console.log('购买成功,获得英雄hero',data+1);
        }
        else{
            console.log('你的钱不够');
        }
        this.initHero();
    },
    initHero:function (params) {
       let heroNode = cc.find("Canvas/bg/heroHouse/heroLayout").getChildren();//获取所有英雄的节点
       for (let j = 0; j < 6; j++) {
        let isHave = this.isHaveHero[j];
        let noHaveHeroNode = heroNode[j].getChildByName("noHaveHero");
        if (isHave) {
            console.log('拥有英雄hero',j+1);
            noHaveHeroNode.active = false;
            heroNode[j].getChildByName("haveHero").active = true;
        }
        else{
            // 获取价格label组件
           let goldNum =  cc.find("Canvas/bg/heroHouse/heroLayout/hero"+(j+1)+"/noHaveHero/gold/goldNum").getComponent(cc.Label);
           goldNum.string = this.heroPrice[j];
        }
       }
    },



    clickLotteryBtn:function () {
        if (!this.isOnLottery) {
            this.freeLotteryNum--;
            if (this.freeLotteryNum>=0) {
                this.Tip_label.string = "剩余免费次数"+this.freeLotteryNum+"次";
                const {result,ragle} = this.getLotteryResult();
                this.ResultPrize = result;
                let prize = this.prizeArray[result];
                let addragle =Math.round( Math.random()*45) + prize[0];
                this.lotteryAction(addragle);
                console.log('你抽到了',result);
            }
            else{
                this.Tip_label.string = "没有免费次数了";
            }
        }
    },
    getLotteryResult:function () {
        let ragle = Math.random()*360 ;
        let result = null;
        
        if(ragle>340){
            result = "一等奖";
            return {result,ragle}
        }
        else if(ragle>300){
            result = "钻石x100";
            return  {result,ragle}
        }
        else if(ragle>250){
            result = "钻石x1";
            return  {result,ragle}
        }
        else if(ragle>200){
            result = "技能加血";
            return  {result,ragle}
        }
        else if(ragle>150){
            result = "全屏爆破";
            return  {result,ragle}
        }
        else if(ragle>100){
            result = "全屏冰冻";
            return  {result,ragle}
        }
        else if(ragle>50){
            result = "全屏破甲";
            return  {result,ragle}
        }
        else if (ragle>0) {
            result = "再来一次";
            return  {result,ragle}
        }
      
    },
    lotteryAction:function (neddRotation) {
        e = this;
        e.isOnLottery = true;
        e.turnRange += 720;
        console.log('开始抽奖，展示动画');
        cc.tween(e.turnPan)
        .to(3,{ angle:(  e.turnRange + neddRotation )}, { easing: 'quartInOut' })
        .call(() => { 
            this.isOnLottery = false;
            this.showLotteryResult();
            console.log('动画播放完');
        })
        .start()
        
    },
    showLotteryResult:function (params) {
        this.resultPage.active = true;
        // this.result_label.node.color = (122,232,345);
        this.result_label.string ="恭喜获得：" + this.ResultPrize;
    },
    update (dt) {

    },
});
